package jmine.tec.utils.reflection;

import jmine.tec.annotations.Unmodifiable;
import jmine.tec.utils.debug.impl.DebugRuntimeFactory;

/**
 * Classe utilitaria para verificar a presenca da annotation {@link Unmodifiable}
 * 
 * @author takeshi
 */
public final class UnmodifiableChecker {

    /**
     * C'tor
     */
    private UnmodifiableChecker() {
        // vazio
    }

    /**
     * Verifica se a classe e todas as superclasses apresentam a anotacao unmodifiable
     * 
     * @param anyType {@link Class}
     * @return boolean
     */
    public static boolean isUnmodifiableAnnotationPresent(Class<?> anyType) {
        if (anyType == null || anyType.isInterface() || anyType.isPrimitive() || anyType.isArray()) {
            return false;
        }
        if (!anyType.isAnnotationPresent(Unmodifiable.class)) {
            return false;
        }
        return anyType.getSuperclass() == Object.class || isUnmodifiableAnnotationPresent(anyType.getSuperclass());
    }

    /**
     * Verifica se uma classe possui a anotacao se o programa esta rodando em debug.
     * 
     * @param anyType {@link Class}
     */
    public static void checkUnmodifiableIfDebug(Class<?> anyType) {
        if (DebugRuntimeFactory.ENABLED) {
            if (!UnmodifiableChecker.isUnmodifiableAnnotationPresent(anyType)) {
                System.err.println("type " + anyType + " is not @Unmodifiable!");
            }
        }
    }

}
